package com.hanhanxiaochong.zhongxiangchuanmei.util;

/**
 * Created by RockPhoenix on 2017/12/25.
 */

import android.support.annotation.NonNull;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Instruction:Rxjava2.x实现定时器
 * <p>
 * Author:pei
 * Date: 2017/6/29
 * Description:
 */

public class RxTimerUtil {

    private static CompositeSubscription compositeSubscription = new CompositeSubscription();

    /**
     * milliseconds毫秒后执行next操作
     *
     * @param milliseconds
     * @param next
     */
    public static void timer(long milliseconds, final IRxNext next) {
        Observable.timer(milliseconds, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onNext(@NonNull Long number) {
                        if (next != null) {
                            next.doNext(number);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        cancel();
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //取消订阅
                        cancel();
                    }
                });
    }


    /**
     * 每隔milliseconds毫秒后执行next操作
     *
     * @param seconds
     * @param next
     */
    public static void interval(long seconds, long timeCount, final IRxNext next) {
        Subscription subscription = Observable.interval(seconds, TimeUnit.SECONDS)
                .take(timeCount, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onNext(@NonNull Long number) {
                        if (next != null) {
                            next.doNext(number);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        next.doComplete();

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }
                });
        compositeSubscription.add(subscription);
    }

    public static void interval(long seconds, final IRxNext next) {
        Subscription subscription = Observable.interval(seconds, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onNext(@NonNull Long number) {
                        if (next != null) {
                            next.doNext(number);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        next.doComplete();

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }
                });
        compositeSubscription.add(subscription);
    }


    /**
     * 取消订阅
     */
    public static void cancel() {
        if (compositeSubscription != null && !compositeSubscription.isUnsubscribed()) {
            compositeSubscription.unsubscribe();
        }
    }

    public interface IRxNext {

        void doNext(long number);

        void doComplete();
    }
}
